package cn.linstudy.mq;

import cn.linstudy.service.ISeckillProductService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Description 秒杀失败的消费者
 * @Author XiaoLin
 * @Date 2021/5/21 16:43
 */
@Component
@Slf4j
@RocketMQMessageListener(
    consumerGroup = "MQSeckillFailListener",
    topic = MQConstant.ORDER_RESULT_TOPIC,
    selectorExpression = MQConstant.ORDER_RESULT_FAIL_TAG
)
public class MQSeckillFailListener implements RocketMQListener<OrderMQResult> {

  @Autowired
  ISeckillProductService seckillProductService;

  @Autowired
  RocketMQTemplate rocketMQTemplate;
  @Override
  public void onMessage(OrderMQResult orderMQResult) {
    log.info("进入到失败队列");
    // 回补预库存，将MySQL中的真实库存查询出来，再去修改redis的真实库存
    seckillProductService.syncRedisStock(orderMQResult.getTime(),orderMQResult.getSeckillId());
    // 修改本地标识，以广播模式，发一个消息
    rocketMQTemplate.syncSend(MQConstant.CANCEL_SECKILL_OVER_SIGE_TOPIC,orderMQResult.getSeckillId());
  }
}
